<?php
namespace cmf\lib\interfaceauth;
use think\Config;

/**
 * rsa 加解密类
 */
class Rsa
{
    
    private static function getPrivateKey()
    {
        return Config::get( 'rsa.private_key' );
    }
    private static function getPublicKey()
    {
        return Config::get( 'rsa.public_key' );
    }
    
    private static function urlsafe_base64_en( $string )
    {
        $data = base64_encode( $string );
        return str_replace( [ '+' , '/' , '=' ] , [ '-' , '_' , '' ] , $data );
    }
    
    private static function urlsafe_base64_de( $string )
    {
        $data = str_replace( [ '-' , '_' ] , [ '+' , '/' ] , $string );
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
        
    }
    
    /**
    *私钥加密
    **/
    public static function privateEncrypt( $data = '' )
    {
        if (!is_string($data)) {            
            return null;       
        }        
        $crypt = "";
        foreach( str_split( $data , 117 ) as $segment ){
            openssl_private_encrypt( $segment , $encrypted , self::getPrivateKey() );
            $crypt .= $encrypted;
        }
        return $encrypted ? self::urlsafe_base64_en($encrypted) : null; 
    }
    
    /**     
     * 公钥加密     
     * @param string $data     
     * @return null|string     
     */    
    public static function publicEncrypt( $data = '' )   
    {        
        if (!is_string($data)) {            
            return null;        
        }     
        $crypt = "";
        foreach( str_split( $data , 117 ) as $segment ){
            openssl_private_encrypt( $segment , $encrypted , self::getPrivateKey() );
            $crypt .= $encrypted;
        }
        return $crypt ? self::urlsafe_base64_en($crypt) : null; 
        
        //return openssl_public_encrypt($data,$encrypted,self::getPublicKey()) ? base64_encode($encrypted) : null;    
    }    

    /**     
     * 私钥解密     
     * @param string $encrypted     
     * @return null     
     */    
    public static function privateDecrypt( $encrypted = '' )    
    {        
        if (!is_string($encrypted)) {            
            return null;        
        }        
        $crypt = "" ;
        foreach( str_split( self::urlsafe_base64_de( $encrypted ) , 128 ) as $segment ){
            openssl_public_decrypt(  $segment , $decrypted, self::getPublicKey() );
            $crypt .= $decrypted;
        }
        return $crypt ? $crypt : null; 
        //return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;    
    }    

    /**     
     * 公钥解密     
     * @param string $encrypted     
     * @return null     
     */    
    public static function publicDecrypt( $encrypted = '' )    
    {        
        if (!is_string( $encrypted ) ) {            
            return null;        
        }        
        $crypt = "" ;
        foreach( str_split( self::urlsafe_base64_de( $encrypted ) , 128 ) as $segment ){
            openssl_public_decrypt(  $segment , $decrypted, self::getPublicKey() );
            $crypt .= $decrypted;
        }
        return $crypt ? $crypt : null;    
    }
}